package com.seven.jdk8;

import com.seven.jdk8.pojo.Employee;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author seven
 */
public class LambdaHelloWorld {
    List<Employee> employeeList = Arrays.asList(
            new Employee(1,"张三",7753.25,30),
            new Employee(2,"李四",1853.25,18),
            new Employee(3,"王五",9753.25,51),
            new Employee(4,"赵六",5653.25,45),
            new Employee(5,"钱七",2553.25,27)
    );
    //需求：查询工资大于5000并且年龄小于50的员工

    //解决方案
    public List<Employee> filterEmployee(List<Employee> employeeList,EmployeeCompare<Employee> empCompare){
        List<Employee> list = new ArrayList<Employee>();
        for(Employee emp:employeeList){
            if(empCompare.compare(emp)){
                list.add(emp);
            }
        }
        return list;
    }

    /**
     * 结果：Employee{id=1, name='张三', salary=7753.25, age=30}
     Employee{id=4, name='赵六', salary=5653.25, age=45}
     */
    @Test
    public void test1(){
        List<Employee> newList = filterEmployee(employeeList, new EmployeeCompare<Employee>() {
            public boolean compare(Employee employee) {
                return employee.getSalary()>5000 && employee.getAge()<50 ;
            }
        });
        for(Employee emp:newList){
            System.out.println(emp);
        }
    }
    //优化解决方案一：使用lambda
    @Test
    public void test2(){
        List<Employee> newList = filterEmployee(employeeList,(e) -> e.getSalary()>5000 && e.getAge()<50 );
        newList.forEach(System.out::println);
    }
    //优化解决方案二：使用lambda+stream
    @Test
    public void test3(){
        employeeList.stream().filter((e) -> e.getSalary()>5000 && e.getAge()<50  ).forEach(System.out::println);
    }
}
